import { usePermission } from '@/hooks/usePermission'
import { readNotice } from '@/api/info/index'
import { useOrderNavigateTo } from '@/hooks/useListNavigate'

export const NoticeTemplates = (e, msg) => {
  console.log('🐳 > NoticeTemplates > e, msg:', e, msg)
  readMsg(msg.msgId) //已读
  if (msg.noticeType == 2) {
    // 系统消息
    uni.navigateTo({
      url: '/pages/info/detail?id=' + msg.msgId,
    })
    return
  }
  switch (e) {
    // 订单
    case 1:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 19:
    case 35:
    case 36:
    case 37:
    case 38:
      navigate2Order(msg, e)
      break
    // OTA
    case 2:
      navigate2OTA(msg, e)
      break
    // 代办
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    case 16:
    case 17:
    case 18:
      navigate2Operation(msg, e)
      break
    // 工单
    case 20:
    case 21:
    case 22:
    case 23:
    case 24:
    case 25:
      navigate2WorkOrder(msg, e)
      break
    // 运营支出
    case 26:
    case 27:
    case 28:
    case 29:
      navigate2OperateCost(msg, e)
      break
    case 30:
      navigate2Stock(msg, e)
      break
    case 33:
      navigate2SyncOtaPrice(msg, e)
      break
    case 34:
      navigate2ThirdPay(msg, e)
      break
    // 无操作消息 9、10、
    default:
      toMsgList()
      break
  }
}

// 权限字符串常量
const PERMISSION_ORDER = 'svmsapp:order:'
const PERMISSION_ORDER_SYNC = 'svmsapp:order-sync'
const PERMISSION_WORK_ORDER = 'svmsapp:work-order'
const PERMISSION_OPERATE_COST = 'svmsapp:operate-cost'
const PERMISSION_STOCK = 'svmsapp:inventory-occupy-record'
const PERMISSION_OTA_PRICE = 'svmsapp:sync-ota-price'

// 辅助函数 - 权限检查
const checkPermission = (permission) => {
  if (!usePermission(permission)) {
    noPermissionHandler()
    return false
  }
  return true
}
// 没权限时,跳转消息中心,并弹窗提示
const noPermissionHandler = () => {
  toMsgList(true)
}

// 批量的参数
const BATCH_PARAMS = {
  1: {
    orderStatus: '100',
  },
  3: {
    orderStatus: '10',
  },
  4: {
    orderLabels: ['今日交车'],
  },
  5: {
    orderLabels: ['今日还车'],
  },
  6: {
    orderLabels: ['逾期未交'],
  },
  7: {
    orderLabels: ['逾期未还'],
  },
  8: {
    orderLabels: ['合同签署中(付费版)'],
  },
  19: {
    orderLabels: ['车辆冲突'],
  },
  35: {
    orderLabels: ['押金未缴纳'],
  },
  36: {
    orderLabels: ['押金自动解冻'],
  },
  37: {
    orderLabels: ['租车押金应退未退'],
  },
  38: {
    orderLabels: ['违章押金应退未退'],
  },
}

// 跳转订单
const navigate2Order = (msg, msg_type) => {
  // 判断权限
  const orderType = msg.orderType == 1 ? 'short-order' : 'long-order'
  if (!checkPermission(PERMISSION_ORDER + orderType)) return
  // 是否是批量；批量查询新订单状态；单条传入关键字
  const params =
    msg.batch == 0
      ? {
          keywords: msg.code,
        }
      : BATCH_PARAMS[msg_type]
  console.log('🐳 > navigate2Order > params:', params, orderType)
  useOrderNavigateTo(params, orderType)
}

// 1. 新订单通知
// 3. 待排车通知
// 4. 即将交车通知
// 5. 即将还车通知
// 6. 逾期未交通知
// 7. 逾期未还通知
// 8. 电子合同未签署
// 19. 车辆冲突

// 9. 账户余额不足通知 (不跳转, 打开应用即可)
// 10. 充值到账通知 (不跳转, 打开应用即可)

// 2. OTA订单同步成功/失败
const navigate2OTA = (msg) => {
  if (!checkPermission(PERMISSION_ORDER_SYNC)) return
  uni.setStorageSync('pushOrderSync', {
    startTime: msg.startTime,
    sourceId: msg.sourceId,
    otaType: msg.otaType,
    status: msg.status,
  })
  uni.navigateTo({
    url: '/pages/order/order-sync-record/index',
  })
}

const TODO_PAGES = {
  11: {
    permission: 'svmapp:1:operation:caroilview',
    url: '/pages/operate/caroilmileage',
  },
  12: {
    permission: 'svmapp:1:operation:mileageview',
    url: '/pages/operate/carmileage',
  },
  13: {
    permission: 'svmapp:1:operation:insureCompulsoryview',
    url: '/pages/operate/jqinsure',
  },
  14: {
    permission: 'svmapp:1:operation:insureBusinessview',
    url: '/pages/operate/businessinsure',
  },
  15: {
    permission: 'svmapp:1:operation:imaintenanceWaitview',
    url: '/pages/operate/upmaintenance',
  },
  16: {
    permission: 'svmapp:1:operation:inspectionWaitview',
    url: '/pages/operate/upyearannually',
  },
  17: {
    permission: 'svmsapp:car-repair',
    url: '/pages/asset/car-repair/list?status=待维修',
  },
  18: {
    permission: 'svmsapp:car-violation',
    url: '/pages/asset/car-violation/list?eventStatus=0',
  },
}
// 11. 车辆油电量异常
// 12. 车辆里程异常
// 13. 交强险即将到期通知
// 14. 商业险即将到期通知
// 15. 即将保养通知
// 16. 即将年检通知
// 17. 待维修车辆通知
// 18. 待处理违章通知

// 跳转运维代办事项
const navigate2Operation = (msg, msg_type) => {
  // 判断权限
  const { permission, url } = TODO_PAGES[msg_type]
  if (!checkPermission(permission)) return
  uni.navigateTo({
    url,
  })
}

// 20.待指派负责人
// 21.待接单
// 22.待审核改派
// 23.申请改派结果通知
// 24. 待处理工单
// 25.负责人拒绝
const WORK_ORDER_BATCH_URLS = {
  20: { url: `/pages/work/workOrder/list?statusList=1` },
  24: { url: `/pages/work/workOrder/list?statusList=3` },
}

// 跳转工单
const navigate2WorkOrder = (msg, msg_type) => {
  // 判断权限
  if (!checkPermission(PERMISSION_WORK_ORDER)) return
  if (msg.batch == 0) {
    uni.navigateTo({
      url: `/pages/work/workOrder/list?workOrderCode=${msg.code}`,
    })
  } else {
    const { url: batch_url } = WORK_ORDER_BATCH_URLS[msg_type]
    uni.navigateTo({
      url: batch_url,
    })
  }
}

// 26.审批待处理通知 27.审批处理结果通知 28.支出单待付款通知 29.支出单完成付款通知
const navigate2OperateCost = (msg, e) => {
  // 判断权限
  if (!checkPermission(PERMISSION_OPERATE_COST)) return
  uni.navigateTo({
    url: `/pages/operate-cost/list?code=${msg.code}`,
  })
}

// 30.库存同步失败
const navigate2Stock = (msg, e) => {
  // 判断权限
  if (!checkPermission(PERMISSION_STOCK)) return
  uni.navigateTo({
    url: `/pages/order/inventory-occupy-record/index?sourceId=${msg.sourceId}&accountId=${msg.accountId}&account=${msg.account}&syncStatus=0`,
  })
}

// 33.反向价格结果
const navigate2SyncOtaPrice = (msg, e) => {
  // 判断权限
  if (!checkPermission(PERMISSION_OTA_PRICE)) return
  uni.navigateTo({
    url: `/pages/sync-ota-price/list`,
  })
}

// 34.三方支付账单
const navigate2ThirdPay = (msg, e) => {
  let permission = ''
  let pages = ''

  switch (msg.orderType) {
    case 1:
      permission = 'svmsapp:order:short-order'
      pages = 'short-order'
      break
    case 2:
      permission = 'svmsapp:order:long-order'
      pages = 'long-order'
      break
    case 3:
      permission = 'svmsapp:car-violation.fee'
      break
  }

  // 判断权限
  if (!checkPermission(permission)) return

  if (msg.orderType == 3) {
    uni.navigateTo({
      url: `/pages/asset/asset-fee/list?feeCategory=4&businesId=${msg.orderId}&orderId=${msg.orderId}&bizType=3&tabIndex=0`,
    })
  } else {
    uni.navigateTo({
      url: `/pages/order/${pages}/detail/index?id=${msg.orderId}&tab=expense&orderId=${msg.orderId}&bizType=${msg.orderType}`,
    })
  }
}

// 已读
const readMsg = (id) => {
  if (!id) return
  readNotice({
    id: id,
  }).then((t) => {
    console.log('点击通知栏已读', t)
  })
}

// 跳转消息列表
const toMsgList = (noPermission = false) => {
  // 防止获取不到pages
  setTimeout(() => {
    var pages = getCurrentPages()
    if (pages.length > 0) {
      var currentRoute = pages[pages.length - 1].route
      if (currentRoute != 'pages/info/list') {
        uni.navigateTo({
          url: '/pages/info/list',
        })
      }
    }

    // 没权限
    if (noPermission) {
      uni.showModal({
        title: '提示',
        content: '暂未分配查看权限',
        showCancel: false,
      })
    }
  }, 300)
}
